import Vue from "vue";
import Vuex from "vuex";
import SockJS from "sockjs-client";
import Stomp from "@stomp/stompjs";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    user: {
      name:
        localStorage.getItem("user" || "[]") == null
          ? "未登录"
          : JSON.parse(localStorage.getItem("user" || "[]")).name,
      userface:
        localStorage.getItem("user" || "[]") == null
          ? ""
          : JSON.parse(localStorage.getItem("user" || "[]")).userface,
      username:
        localStorage.getItem("user" || "[]") == null
          ? ""
          : JSON.parse(localStorage.getItem("user" || "[]")).username,
      roles:
        localStorage.getItem("user" || "[]") == null
          ? ""
          : JSON.parse(localStorage.getItem("user" || "[]")).roles
    },
    routes: [],
    msgList: [],
    isDotMap: new Map(),
    currentFriend: {},
    stomp: null,
    nfDot: false
  },
  mutations: {
    initMenu(state, menus) {
      state.routes = menus;
    },
    login(state, user) {
      state.user = user;
      localStorage.setItem("user", JSON.stringify(user));
    },
    logout(state) {
      localStorage.removeItem("user");
      state.routes = [];
    },
    toggleNFDot(state, newValue) {
      state.nfDot = newValue;
    },
    updateMsgList(state, newMsgList) {
      state.msgList = newMsgList;
    },
    updateCurrentFriend(state, newFriend) {
      state.currentFriend = newFriend;
    },
    addValue2DotMap(state, key) {
      state.isDotMap.set(key, "您有未读消息");
    },
    removeValueDotMap(state, key) {
      state.isDotMap.delete(key);
    }
  },
  actions: {
    connect(context) {
      context.state.stomp = Stomp.over(new SockJS("/ws/endpointChat"));
      context.state.stomp.connect(
        {},
        frame => {
          context.state.stomp.subscribe("/user/queue/chat", message => {
            var msg = JSON.parse(message.body);
            var oldMsg = localStorage.getItem(
              context.state.user.username + "#" + msg.from
            );
            if (oldMsg == null) {
              oldMsg = [];
              oldMsg.push(msg);
              localStorage.setItem(
                context.state.user.username + "#" + msg.from,
                JSON.stringify(oldMsg)
              );
            } else {
              var oldMsgJson = JSON.parse(oldMsg);
              oldMsgJson.push(msg);
              localStorage.setItem(
                context.state.user.username + "#" + msg.from,
                JSON.stringify(oldMsgJson)
              );
            }
            if (msg.from != context.state.currentFriend.username) {
              context.commit(
                "addValue2DotMap",
                "isDot#" + context.state.user.username + "#" + msg.from
              );
            }
            //更新msgList
            var oldMsg2 = localStorage.getItem(
              context.state.user.username +
                "#" +
                context.state.currentFriend.username
            );
            if (oldMsg2 == null) {
              context.commit("updateMsgList", []);
            } else {
              context.commit("updateMsgList", JSON.parse(oldMsg2));
            }
          });
          context.state.stomp.subscribe("/topic/nf", message => {
            context.commit("toggleNFDot", true);
          });
        },
        failedMsg => {}
      );
    }
  }
});
